home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 351-375 / disk_351 / pdc / libsrc.lzh / LibSrc / SysIO / malloc.c < prev    next >
C/C++ Source or Header  |  1990-04-07  |  4KB  |  153 lines

  1. /*
  2.  * Libraries and headers for PDC release 3.3 (C) 1989 Lionel Hummel.
  3.  * PDC Software Distribution (C) 1989 Lionel Hummel and Paul Petersen.
  4.  * PDC I/O Library (C) 1987 by J.A. Lydiatt.
  5.  *
  6.  * This code is freely redistributable upon the conditions that this 
  7.  * notice remains intact and that modified versions of this file not
  8.  * be included as part of the PDC Software Distribution without the
  9.  * express consent of the copyright holders.  No warrantee of any
  10.  * kind is provided with this code.  For further information, contact:
  11.  *
  12.  *  PDC Software Distribution    Internet:                     BIX:
  13.  *  P.O. Box 4006             or hummel@cs.uiuc.edu            lhummel
  14.  *  Urbana, IL  61801-8801       petersen@uicsrd.csrd.uiuc.edu
  15.  */
  16.  
  17. /* malloc - routines for managing memory
  18.  *
  19.  * calloc     allocates a cleared array of blocks
  20.  * free       frees a previously allocated block of memory
  21.  * malloc     allocates a block of memory
  22.  * realloc    alter buffer size; same contents, new ptr 
  23.  */
  24.  
  25. #include <exec/types.h>
  26. #include <exec/memory.h>
  27. #include <functions.h>
  28.  
  29. /* According to X3J11, the response to a request for zero bytes is
  30.    implementation defined.  For PDC, the intention is to return a
  31.    successful result, but one that will cause a trap if it is ever
  32.    dereferenced.  Under most present-day Amiga's, 0xFFFFFFFF (-1L)
  33.    fills the bill just fine.  This may have problems, though, on
  34.    systems with MMU's and very high memory addresses.
  35.  */
  36.  
  37. #define ZERO_REQUEST -1L
  38.  
  39. typedef struct memchunk {
  40.     struct memchunk    *next;
  41.     struct memchunk    *prev;
  42.     long                size;
  43.     } MEMCHUNK;
  44.  
  45. extern void  (*_freeall)();
  46.  
  47. static MEMCHUNK sentinel = {&sentinel, &sentinel, 0};
  48.  
  49. /*
  50.  * Called by exit() to free any allocated memory.
  51.  */
  52.  
  53. static void freeall()
  54. {
  55.     MEMCHUNK    *mp, *mp1;
  56.  
  57.     for ( mp = sentinel.prev; mp != &sentinel; ) {
  58.         mp1 = mp->prev;
  59.         FreeMem( (APTR) mp, (ULONG) mp->size );
  60.         mp = mp1;
  61.     }
  62. }
  63.  
  64.  
  65. char *malloc( size )
  66. unsigned size;
  67. {
  68.     MEMCHUNK *mp;
  69.     ULONG chunksize;    
  70.  
  71.     if (size == 0)
  72.         return(ZERO_REQUEST);
  73.  
  74.     chunksize = size + sizeof(MEMCHUNK);
  75.     mp = AllocMem( chunksize, (ULONG) MEMF_CLEAR);
  76.     if ( mp == NULL )
  77.         return NULL;
  78.  
  79. /* Keep the forward and backward links.
  80.  */
  81.     sentinel.prev->next = mp;
  82.     mp->prev = sentinel.prev;
  83.     sentinel.prev = mp;
  84.  
  85.     mp->next = &sentinel;
  86.     mp->size = chunksize;
  87.     _freeall = &freeall;
  88.     return ++mp;
  89. }
  90.  
  91.  
  92. void free( p )
  93. char *p;
  94. {
  95.     MEMCHUNK *mp, *prevmp, *nextmp;
  96.  
  97.     mp = p - sizeof(MEMCHUNK);
  98.  
  99. /* Sanity check: the prev link should point to us. Do nothing if bad.
  100.  */
  101.     prevmp = mp->prev;
  102.     nextmp = mp->next;
  103.     if ( prevmp->next != mp ) {
  104.         return;
  105.     }
  106.  
  107.     FreeMem( (APTR) mp, (ULONG) mp->size );
  108.     prevmp->next = nextmp;
  109.     nextmp->prev = prevmp;
  110.  
  111.     return;
  112. }
  113.  
  114.  
  115. char *calloc(nelem, elsize)
  116. unsigned int nelem, 
  117.              elsize;
  118. {
  119.     register char *newmem;
  120.     register int   totsize;
  121.  
  122.     totsize = nelem * elsize;
  123.     newmem = malloc(totsize);
  124.     bzero(newmem, totsize);
  125.     
  126.     return(newmem);
  127. }
  128.  
  129.  
  130. char *realloc(ptr, size)
  131. char *ptr;
  132. unsigned size;
  133. {
  134.     MEMCHUNK *mp, *prevmp;
  135.     char *newmem;
  136.  
  137.     mp = (MEMCHUNK *) (ptr - sizeof(MEMCHUNK));
  138.  
  139. /* Sanity check: the prev link should point to us. Do nothing if bad.
  140.  */
  141.     prevmp = mp->prev;
  142.     if ( prevmp->next != mp ) {
  143.         return(0);
  144.     }
  145.  
  146.     newmem = malloc(size);
  147.     size = ( (size > mp->size) ? mp->size : size);
  148.  
  149.     bcopy(ptr, newmem, size);
  150.  
  151.     return(newmem);
  152. }
  153.